package com.situ.mvc.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.situ.mvc.entity.Student;
import com.situ.mvc.entity.StudentTeacherBean;
import com.situ.mvc.entity.Teacher;
import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader.Array;

// /student/add.action
@Controller
@RequestMapping(value="/student")
public class StudentController {
	
	@RequestMapping(value="/getStudentAddPage")
	public String getStudentAddPage() {
		// req.getRequestDispatcher("/WEB-INF/jsp/student_add.jsp").forward(req,resp);
		//return "/WEB-INF/jsp/student_add.jsp";
		return "student_add";
	}
	
	@RequestMapping(value="/add")
	public ModelAndView add(Student student) {
		// String age = request.getParameter("age");
		System.out.println(student);
		// request.setAttirbute("student", student);
		// Model域对象存放数据，View处理界面
		ModelAndView modelAndView = new ModelAndView();
		//将数据放到域对象，就是放到request这个域对象中
		modelAndView.addObject("student", student);
		// request.getRequestDispatcher("/student_info.jsp").forward(req,resp);
		//转发到/student_info.jsp
		//modelAndView.setViewName("/student_info.jsp");
		modelAndView.setViewName("student_info");
		return modelAndView;
	}
	
	//默认情况下既可以处理get请求也可以处理post请求
	//如果希望这个方法只处理post请求，method=RequestMethod.POST
	@RequestMapping(value="/add2", method=RequestMethod.POST)
	public String add2(Student student, Model model) {
		System.out.println("StudentController.add2()");
		// String age = request.getParameter("age");
		System.out.println(student);
		// request.setAttirbute("student", student);
		model.addAttribute("student", student);
		
		// request.getRequestDispatcher("/student_info.jsp").forward(req,resp);
		return "student_info";
	}
	
	@RequestMapping(value="/add3", method=RequestMethod.POST)
	public void add3(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("StudentController.add3()");
		String name = request.getParameter("name");
		String age = request.getParameter("age");
		String gender = request.getParameter("gender");
		Student student = new Student(name, Integer.parseInt(age), gender);
		//得到session
		HttpSession session = request.getSession();
		request.setAttribute("student", student);
		request.getRequestDispatcher("/WEB-INF/jsp/student_info.jsp").forward(request, response);
	}
	
	@RequestMapping(value="/add4")
	public String add4(String name, Integer age, String gender, Model model) {
		System.out.println("name: " + name);
		System.out.println("age: " + age);
		System.out.println("gender: " + gender);
		model.addAttribute("name", name);
		model.addAttribute("age", age);
		model.addAttribute("gender", gender);
		
		return "student_info";
	}
	
	// /student/deleteById.action?id=12
	@RequestMapping(value="/deleteById")
	public String deleteById(Integer id, Model model) {
		System.out.println("id: " + id);
		model.addAttribute("id", id);
		
		//return "student_info"; //转发
		//转发到/student/pageList.action
		//return "forward:/student/pageList.action";
		//response.sendRedirect("/Java1812SpringMVC/student/pageList.aciton");
		return "redirect:/student/pageList.action";
	}
	
	@RequestMapping(value="/pageList1")
	public String pageList1(Model model) {
		System.out.println("StudentController.pageList()");
		List<Student> list = new ArrayList<>();
		list.add(new Student("张三1", 21, "男"));
		list.add(new Student("张三2", 21, "男"));
		model.addAttribute("list", list);
		return "student_list";
	}
	
	@RequestMapping(value="/getStudentTeacherAddPage")
	public String getStudentTeacherAddPage() {
		return "student_teacher_add";
	}
	
	@RequestMapping(value="/addStudentTeacher")
	public String addStudentTeacher(StudentTeacherBean studentTeacherBean, Model model) {
		System.out.println(studentTeacherBean.getStudent());
		System.out.println(studentTeacherBean.getTeacher());
		
		return "student_info";
	}
	
	@RequestMapping(value="/deleteAll")
	public void deleteAll(Integer[] ids, HttpServletRequest request) {
		// /student/deleteAll.action?ids=1&ids=2&ids=3
		String[] idArray = request.getParameterValues("ids");
		System.out.println(Arrays.toString(idArray));//[2, 3, 4]
		System.out.println(Arrays.toString(ids));
	}
	
	@RequestMapping(value="/list")
	@ResponseBody
	public List<Student> list() {
		List<Student> list = new ArrayList<>();
		for (int i = 0; i < 100; i++) {
			Student student = new Student(i, "张" + i, 20, "男");
			list.add(student);
		}
		
		return list;
	}
	
	@RequestMapping(value="/findById")
	@ResponseBody
	public Student findById(Integer id) {
		Student student = new Student(1, "李四", 20, "男");
		return student;
	}
	
	@RequestMapping(value="/check")
	@ResponseBody
	public boolean checkName() {
		boolean isExist = true;
		return isExist;
	}
	
	@RequestMapping(value="/check1")
	@ResponseBody
	public Map<String, Object> checkName1() {
		boolean isExist = true;
		Map<String, Object> map = new HashMap<>();
		map.put("isExist", isExist);
		map.put("age", 30);
		return map;
	}
	
	@RequestMapping(value="/pageList")
	public void pageList(@RequestParam(value="pageNo", required=true/*, defaultValue="1"*/)Integer pageNo, @RequestParam(defaultValue="3")Integer pageSize) {
		System.out.println("pageNo: " + pageNo);
		System.out.println("pageSize: " + pageSize);
	}
	
}
